<?php
require_once 'Zend/Controller/Front.php';

require_once 'Zend/Loader.php';

require_once 'Zend/Session/Namespace.php';

class Impact_Search
{
    protected static $_namePrefix = '';

    protected static $_searchClass = 'Impact_Search';

    protected $_name;

    protected $_fields = null;

    public function __construct($name = null)
    {
        if (null === $name) {
            $this->_name = self::_getDefaultName();
        } else {
            $this->_name = $name;
        }
    }

    public static function setSearchClassName($name)
    {
        self::$_searchClass = $name;
    }

    public static function getSearchClassName()
    {
        return self::$_searchClass;
    }

    public static function setNamePrefix($namePrefix)
    {
        self::$_namePrefix = $namePrefix;
    }

    public static function getNamePrefix()
    {
        return self::$_namePrefix;
    }

    public static function get($name = null)
    {
        if (null === $name) {
            $name = self::_getDefaultName();
        }

        $namespace = new Zend_Session_Namespace($name);

        if ($namespace->search instanceof Impact_Search) {
            return $namespace->search;
        } else {
            Zend_Loader::loadClass(self::$_searchClass);
            $class = new ReflectionClass(self::$_searchClass);

            $namespace->search = $class->newInstance($name);

            return $namespace->search;
        }
    }

    public function update($defaultValues)
    {
        if (null === $this->_fields) {
            $this->_fields = $defaultValues;
        }

        if ($this->_getUpdateParam('search') != $this->_name) {
            return;
        }

        if ($this->_updateParamExists('clear')) {
            $this->_fields = $defaultValues;
        } else {
            foreach ($defaultValues as $key => $value) {
                if ($this->_updateParamExists($key)) {
                    $updateParam = $this->_getUpdateParam($key);

                    if (is_array($value) and is_array($updateParam)) {
                        $this->_fields[$key] = $updateParam;
                    } else if (is_string($value) and is_string($updateParam)) {
                        $this->_fields[$key] = $updateParam;
                    }
                }
            }
        }
    }

    public function getName()
    {
        return $this->_name;
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->_fields)) {
            return $this->_fields[$name];
        } else {
            return '';
        }
    }

    public function toArray()
    {
        return $this->_fields;
    }

    protected static function _getDefaultName()
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();

        return self::$_namePrefix
            . $request->getControllerName() 
            . '-' 
            . $request->getActionName();
    }

    protected function _updateParamExists($name)
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        return null !== $request->getParam($name);
    }

    protected function _getUpdateParam($name)
    {
        $request = Zend_Controller_Front::getInstance()->getRequest();
        return $request->getParam($name, '');
    }
}
// vim: sw=4:ts=4:sts=4:et
